﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using XGameLibrary;
using XGame.Class;
using XGameLibrary.Sprite;
using XGameLibrary.Character;
using XGameLibrary.Controls;
using TomShane.Neoforce.Controls;
using System.Collections.ObjectModel;


namespace XGame.GameScreens
{
    public class GamePlayScreen : XGameScreenBase
    {
        private Player player;
        private World world;
        private MaskData maskData;
        private int cellSize;
        ToolCell fromCell;
        bool isMoveEnd;
        LinkLabel loadGame;


        public GamePlayScreen(Manager manager, GameScreenManager screenManager)
            : base(manager, screenManager)
        {
            cellSize = 20;
            isMoveEnd = false;
        }

        #region XNA Method Region

        public override void Initialize()
        {






            RoleAttributeBack roleAttributeBack;
            roleAttributeBack = new RoleAttributeBack(manager);
            roleAttributeBack.Init();
            roleAttributeBack.Left = 20;
            roleAttributeBack.Top = 120;
            roleAttributeBack.Visible = false;
            manager.Add(roleAttributeBack);


            ParcelBody parcelBody;
            parcelBody = new ParcelBody(manager);
            parcelBody.Init();
            parcelBody.Left = 512;
            parcelBody.Top = 190;
            parcelBody.Visible = false;
            manager.Add(parcelBody);

            BottomMenu bottommenu;
            bottommenu = new BottomMenu(manager);
            bottommenu.Init();
            bottommenu.Left = (InputHandler.ScreenWidth - bottommenu.Width) / 2;
            bottommenu.Top = InputHandler.ScreenHeight - bottommenu.Height;
            bottommenu.RoleAttributeButton.Click += delegate(object sender, TomShane.Neoforce.Controls.EventArgs e)
            {
                ImageButton ib = (ImageButton)sender;
                roleAttributeBack.Visible = ib.Pushed;
            };
            bottommenu.ParcelBodyButton.Click += delegate(object sender, TomShane.Neoforce.Controls.EventArgs e)
            {
                ImageButton ib = (ImageButton)sender;
                parcelBody.Visible = ib.Pushed;
            };
            manager.Add(bottommenu);

            ItemBar itembar;
            itembar = new ItemBar(manager);
            itembar.Init();
            itembar.Left = bottommenu.Left + 50;
            itembar.Top = bottommenu.Top - 15;
            manager.Add(itembar);

            ToolIcon ti = new ToolIcon(manager, ToolIcon.Type.Head, "头部装备", "随便描述一下");
            ti.Init();
            ti.Image = manager.Content.Load<Texture2D>(@"Content\Image\Icon\56");
            ti.Movable = true;
            ti.MoveBegin += new TomShane.Neoforce.Controls.EventHandler(ToolIcon_MoveBegin);
            ti.MoveEnd += new TomShane.Neoforce.Controls.EventHandler(ToolIcon_MoveEnd);
            ti.MouseUp += new MouseEventHandler(ToolIcon_MouseUp);
            ToolIcon ti1 = new ToolIcon(manager, ToolIcon.Type.Neck, "颈部装备", "随便描述一下");
            ti1.Init();
            ti1.Image = manager.Content.Load<Texture2D>(@"Content\Image\Icon\63");
            ti1.Movable = true;
            ti1.MoveBegin += new TomShane.Neoforce.Controls.EventHandler(ToolIcon_MoveBegin);
            ti1.MoveEnd += new TomShane.Neoforce.Controls.EventHandler(ToolIcon_MoveEnd);
            ti1.MouseUp += new MouseEventHandler(ToolIcon_MouseUp);
            ToolIcon ti3 = new ToolIcon(manager, ToolIcon.Type.Item, "血瓶", "加点血");
            ti3.Init();
            ti3.Image = manager.Content.Load<Texture2D>(@"Content\Image\Icon\67");
            ti3.Movable = true;
            ti3.MoveBegin += new TomShane.Neoforce.Controls.EventHandler(ToolIcon_MoveBegin);
            ti3.MoveEnd += new TomShane.Neoforce.Controls.EventHandler(ToolIcon_MoveEnd);
            ti3.MouseUp += new MouseEventHandler(ToolIcon_MouseUp);
            ToolIcon ti4 = new ToolIcon(manager, ToolIcon.Type.Item, "蓝瓶", "加点蓝");
            ti4.Init();
            ti4.Image = manager.Content.Load<Texture2D>(@"Content\Image\Icon\68");
            ti4.Movable = true;
            ti4.MoveBegin += new TomShane.Neoforce.Controls.EventHandler(ToolIcon_MoveBegin);
            ti4.MoveEnd += new TomShane.Neoforce.Controls.EventHandler(ToolIcon_MoveEnd);
            ti4.MouseUp += new MouseEventHandler(ToolIcon_MouseUp);
            parcelBody.Cells[0, 0].Icon = ti3;
            parcelBody.Cells[1, 1].Icon = ti4;
            parcelBody.Cells[0, 1].Icon = ti;
            parcelBody.Cells[4, 7].Icon = ti1;
            ToolIcon ti2 = new ToolIcon(manager, ToolIcon.Type.Head, "头部装备", "随便描述一下");
            ti2.Init();
            ti2.Image = manager.Content.Load<Texture2D>(@"Content\Image\Icon\57");
            ti2.Movable = true;
            ti2.MoveBegin += new TomShane.Neoforce.Controls.EventHandler(ToolIcon_MoveBegin);
            ti2.MoveEnd += new TomShane.Neoforce.Controls.EventHandler(ToolIcon_MoveEnd);
            ti2.MouseUp += new MouseEventHandler(ToolIcon_MouseUp);
            roleAttributeBack.HeadCell.Icon = ti2;

            base.Initialize();
        }


        void ToolIcon_MoveEnd(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            isMoveEnd = true;
        }

        void ToolIcon_MouseUp(object sender, MouseEventArgs e)
        {
            if (isMoveEnd)
            {
                ToolIcon icon = sender as ToolIcon;
                ToolCell toCell = null;
                Collection<ToolCell> controls = InputHandler.HitTest<ToolCell>(manager.Controls, e.State.X, e.State.Y);
                if (controls != null)
                    toCell = controls.ElementAt(0);
                if (toCell != null && toCell.ContainType(icon.IconType))
                {
                    if (toCell.Icon != null)
                    {
                        ToolIcon icon1 = toCell.Icon;
                        if (icon1 != null)
                            fromCell.Icon = icon1;
                    }
                    toCell.Icon = icon;
                }
                else
                    fromCell.Icon = icon;
                isMoveEnd = false;
            }
        }


        void ToolIcon_MoveBegin(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            ToolIcon icon = sender as ToolIcon;
            TomShane.Neoforce.Controls.Control c = icon.Parent;
            while (!(c is ToolCell))
                c = c.Parent;
            fromCell = c as ToolCell;
            if (fromCell != null)
            {
                fromCell.Icon = null;
                manager.Add(icon);
                icon.Left = fromCell.OriginLeft;
                icon.Top = fromCell.OriginTop;
            }
        }

        protected override void LoadContent()
        {
            CreateWorld();
            CreatePlayer();
            base.LoadContent();

        }

        private void CreatePlayer()
        {
            Texture2D[,] texture = new Texture2D[Animation.DirectionCount, Animation.FrameCount];
            for (int i = 0; i < Animation.DirectionCount; i++)
            {
                for (int j = 0; j < Animation.FrameCount; j++)
                {
                    texture[i, j] = this.Game.Content.Load<Texture2D>(string.Format(@"Image\Role\Body\0\{0}-{1}", i, j));
                }
            }

            Dictionary<Animation.Direction, Animation> animations = new Dictionary<Animation.Direction, Animation>();

            Animation animation = new Animation(100, 100);
            animations.Add(Animation.Direction.North, animation);
            animation = new Animation(100, 100);
            animations.Add(Animation.Direction.NorthEast, animation);
            animation = new Animation(100, 100);
            animations.Add(Animation.Direction.East, animation);
            animation = new Animation(100, 100);
            animations.Add(Animation.Direction.SouthEast, animation);
            animation = new Animation(100, 100);
            animations.Add(Animation.Direction.South, animation);
            animation = new Animation(100, 100);
            animations.Add(Animation.Direction.SouthWest, animation);
            animation = new Animation(100, 100);
            animations.Add(Animation.Direction.West, animation);
            animation = new Animation(100, 100);
            animations.Add(Animation.Direction.NorthWest, animation);


            AnimatedSprite sprite = new AnimatedSprite(texture, animations);
            sprite.Position = new Vector2(InputHandler.ScreenWidth / 2, InputHandler.ScreenHeight / 2);

            Character character = new Character(sprite);

            Texture2D fp = this.Game.Content.Load<Texture2D>(@"Image\Interface\Plate\LeaderFacePlate");
            Texture2D face = this.Game.Content.Load<Texture2D>(@"Image\Interface\Face\0_0");
            Texture2D battle = this.Game.Content.Load<Texture2D>(@"Image\Interface\PKMode\0");

            player = new Player(manager, GameRef, character, maskData, fp, face, battle);

            //loadGame = new LinkLabel(manager);
            //loadGame.Text = player.Sprite.Position.X + ":" + player.Sprite.Position.Y;
            //loadGame.Size = loadGame.SpriteFont.MeasureString(loadGame.Text);
        }

        private void CreateWorld()
        {
            Texture2D background = Game.Content.Load<Texture2D>(@"Image\Map\3\Map\Map4");
            //Map layer = new Map(GameRef.ScreenRectangle.Width, GameRef.ScreenRectangle.Height);

            world = new World(GameRef, background, 20);
            Point[] points = new Point[18];
            for (int i = 0; i < points.Length; i++)
            {
                points[i] = new Point(i, 12);
            }
            maskData = new MaskData(background.Width, background.Height, cellSize, points);
        }

        public override void Update(GameTime gameTime)
        {
            bool ismouseclick = ControlManager.Update(gameTime);
            world.Update(gameTime);
            if (!ismouseclick)
                player.Update(gameTime);

            //loadGame.Text = player.Sprite.Position.X + ":" + player.Sprite.Position.Y;
            //loadGame.Size = loadGame.SpriteFont.MeasureString(loadGame.Text);

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            //GameRef.SpriteBatch.Begin(
            //    SpriteSortMode.Deferred,
            //    BlendState.AlphaBlend,
            //    SamplerState.PointClamp,
            //    null,
            //    null,
            //    null,
            //    player.Camera.Transformation);
            GameRef.SpriteBatch.Begin();

            base.Draw(gameTime);

            world.DrawLevel(gameTime, GameRef.SpriteBatch, player.Camera);
            maskData.Draw(gameTime, GameRef.GraphicsDevice, GameRef.SpriteBatch, player.Camera);
            player.Draw(gameTime, GameRef.GraphicsDevice, GameRef.SpriteBatch);
            //loadGame.Draw(GameRef.SpriteBatch);

            GameRef.SpriteBatch.End();
        }

        #endregion

    }
}
